Docker Kata 4: Running a Web Server in a Container

Learn to run a web server in a container and view its default page.

Containers are useful as components of complex information systems. Web servers are, of course, an important component of any web-based application. This kata will demonstrate how containers can be used to run a web server.

Step 1: Run a web server#

The command to stop and remove all the containers is given below.

The output will be something like this:

Stopping and removing all the containers

Commands

Parameter

Description

docker container stop $(docker container ls -q)

This stops and removes all the containers.

docker container rm $(docker container ls -aq)

These commands stop and remove all the containers.

The command to run a disconnected NGINX container is given below.

When we execute the command above, the output will be something like this:

Running the disconnected NGINX container

Commands

Parameter

Description

container

This is the parent command.

run

This runs a container.

-d

This runs a container in disconnected mode.

-p

This maps a TCP port from the host to the container.

80:80

This publishes a port from the container to the host. The format is host port:container port. Requests sent to the specified host port are forwarded to the container port.

--name

This assigns a name to a container.

webserver

This is the name assigned to the container.

nginx

This is the name of the image to run.

Previous katas have used the NGINX container as a demonstration of a container that runs in disconnected mode. This kata shows an NGINX container doing what it’s designed to do: run an HTTP server.

Commands like this have been run in previous katas. The new parameter here is -p, which publishes a container port to a host port.

The command to view the NGINX default page is given below:

The result of the command above will be something like this:

Viewing the NGINX default page

Command

Parameter/Command

Description


curl localhost

curl: This is a command-line program that sends HTTP requests to an endpoint.

localhost: This is an alias for the local network adapter. This command sends a request to the local machine on port 80, which is the default HTTP port.

The curl program is a simple command-line HTTP client. This command uses curl to issue a request to the NGINX server from the command line.

The output will be something like this:

NGINX browser page

The last step demonstrates that the NGINX server can also be accessed by a web browser.

Step 2: Run a second web server on a different port#

The command to run a second disconnected NGINX server is given below.

The result of the command above after execution will be something like this:

Running a second disconnected NGINX server

Commands

Parameter

Description

docker container

This is the parent command.

run

This runs a container.

-d

This runs a container in disconnected mode.

-p

This maps a TCP port from the host to the container.

81:80

This publishes a port from the container to the host. The format is host port:container port. Requests sent to the specified host port are forwarded to the container port.

--name

This assigns a name to a container.

webserver2

This is the name assigned to the container.

nginx

This is the name of the image to run.

Both NGINX containers are now running, one mapped to host port 80, the other to host port 81. We should be able to use curl, alternating between ports 80 and 81, to view either web server. Port publishing, or mapping, can be used for a variety of use cases. Multiple web server containers can be run side by side on a single IP address, to distribute load, or to run different versions of the same application at the same time.

The command to view the NGINX default page is given below.

The output will be something like this:

Viewing the NGINX default page

Command

Parameter/Command

Description

curl localhost:81

curl: This is a command-line program that sends HTTP requests to an endpoint.

localhost: This is an alias for the local network adapter. This command specifies port 81 (instead of the default port 80).

This command demonstrates that the second NGINX container is indeed running and mapped to host port 81. We can also use Firefox to view the page by visiting localhost:81.

Practice commands#

We’ve given a terminal and table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!

Commands

Step

Command

This runs a disconnected NGINX container named webserver with port 80 mapped between the container and host.


docker container run -d -p 8080:80 --name webserver nginx

This views the NGINX default page using curl.

curl localhost:8080

This runs a second disconnected NGINX server named webserver2 with host port 81 mapped to container port 80.


docker container run -d -p 81:80 --name webserver2 nginx

This views the default page of the webserver2 NGINX container using curl.

curl localhost:81

This stops and removes all the containers.

docker container stop $(docker container ls -q)

docker container rm $(docker container ls -aq)

/
main.txt
Running a web server in a container

Docker Kata 3: Container Volumes and File System

Docker Kata 5: Docker Networking